package com.css.zfzx.sjcj.modules.drill.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY02Entity;
import com.css.zfzx.sjcj.modules.drill.service.QhY02Service;
import com.css.zfzx.sjcj.modules.drill.viewobjects.QhY02QueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Li Helei
 * @date 2020-11-11
 */
@Slf4j
@RestController
@RequestMapping("/qh/qhY02")
public class QhY02Controller {
    @Autowired
    private QhY02Service qhY02Service;

    @GetMapping("/queryQhY02s")
    public RestResponse queryQhY02s(HttpServletRequest request, QhY02QueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String sort= request.getParameter("sort");
            String order= request.getParameter("order");
            JSONObject jsonObject = qhY02Service.queryQhY02s(queryParams, curPage, pageSize,sort,order);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/queryExpertQhY02s")
    public RestResponse queryExpertQhY02s(HttpServletRequest request, QhY02QueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String sort= request.getParameter("sort");
            String order= request.getParameter("order");
            JSONObject jsonObject = qhY02Service.queryExpertQhY02s(queryParams, curPage, pageSize,sort,order);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    @GetMapping("/queryQhY02sNoPage")
    public RestResponse queryQhY02sNoPage(QhY02QueryParams queryParams) {
        RestResponse response = null;
        try {
            JSONObject jsonObject = qhY02Service.queryQhY02s(queryParams);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getQhY02(@PathVariable String id) {
        RestResponse response = null;
        try {
            QhY02Entity qhY02 = qhY02Service.getQhY02(id);
            response = RestResponse.succeed(qhY02);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/save")
    public RestResponse saveQhY02(@RequestBody QhY02Entity qhY02) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            qhY02Service.saveQhY02(qhY02);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    @PostMapping("/update")
    public RestResponse updateQhY02(@RequestBody QhY02Entity qhY02) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            qhY02Service.updateQhY02(qhY02);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/delete")
    public RestResponse deleteQhY02s(@RequestBody String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            //qhY02Service.deleteQhY02s(ids);
            qhY02Service.deleteQh(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(qhY02Service.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    @PostMapping("/submit/{ids}")
    public RestResponse submitQhY02(@PathVariable String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            qhY02Service.submitQhY02(ids);
            json.put("message", "提交成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "提交失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 坐标精度校验
     */
    @PostMapping("/checkCoordinate/{ids}")
    public RestResponse checkCoordinate(@PathVariable String ids) {
        RestResponse restResponse = null;
        try {
            JSONObject json = qhY02Service.checkCoordinate(ids);
            restResponse = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMsg = "坐标精度校验失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 系统质检
     */
    @PostMapping("/dataQualityCheck/{ids}")
    public RestResponse dataQualityCheck(@PathVariable String ids){
        RestResponse restResponse = null;
        try {
            JSONObject jsonObject = qhY02Service.dataQualityCheckAll(ids);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMsg = "质检失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
}